package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**科室信息
 * @author Administrator
 *
 */
public class HospitalTwo extends Activity {

	private CustomProgressDialog proDialog;// 加载框
	private HospitalTwo oThis = this;
	private List<Map<String, Object>> list_ks;
	private String strYyId, strKsId;
	private Intent mIntent;
	private ListView lv;
	private MyAdapter myadapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hospital_two);
		MyApplication myApplication;
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		// 获取控件
		findView();
		
		// 获取数据
		getData();

	}

	private void findView() {
		oThis = this;
		list_ks = new ArrayList<Map<String, Object>>();
		mIntent = getIntent();
		if (mIntent != null) {
			strYyId = mIntent.getStringExtra("strId");
		}
		
		lv = (ListView) findViewById(R.id.list_kssub);
		
	}

	private void getData() {
		proDialog = CustomProgressDialog.createDialog(this);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();
	}

	class LoadData implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Looper.prepare();
			iniKs();
			Looper.loop();
		}
	}

	private void iniKs() {
		try {
			Message msg = myHandler.obtainMessage();
			Bundle bundle = new Bundle();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			ls.add(new BasicNameValuePair("id", strYyId));
			JSONObject json = HTTPGetTool.getTool().post(
					URLUtils.HOST + URLUtils.HOSPITALNAME, ls);

			if (json != null && !json.equals("")) {
				if (!json.toString().equals(
						"{\"msg\":\"系统错误！\",\"success\":\"false\"}")) {
					if (!json.getString("nodeinfo").equals("")) {
						// 获取收费信息
						JSONArray jsonArr = new JSONArray(
								json.getString("nodeinfo"));
						JSONObject js = null;
						for (int i = 0; i < jsonArr.length(); i++) {
							Map<String, Object> map_ks = new HashMap<String, Object>();
							Map<String, Object> map_kssx = new HashMap<String, Object>();
							js = jsonArr.getJSONObject(i);
							map_ks.put("ks", js.getString("orgname"));
							map_ks.put("ksId", js.getString("id"));
							map_ks.put("kssx", js.getString("sxnr"));
							list_ks.add(map_ks);
						}
						bundle.putInt("type", 0);
						bundle.putString("msg", "");
					} else {
						bundle.putInt("type", -1);
						bundle.putString("msg", "暂无数据");
					}

				} else {
					bundle.putInt("type", -1);
					bundle.putString("msg", "系统错误");
				}

			} else {
				bundle.putInt("type", -1);
				bundle.putString("msg", "网络连接失败!");
			}
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 捕捉异常
			Bundle bundle = new Bundle();
			bundle.putInt("type", -1);
			bundle.putString("msg", e.toString());
			Message msg = myHandler.obtainMessage();
			msg.setData(bundle);
			myHandler.sendMessage(msg);
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			int type = msg.getData().getInt("type");
			switch (type) {
			case -1:
				proDialog.dismiss();
				Toast.makeText(oThis, msg.getData().getString("msg"),
						Toast.LENGTH_SHORT).show();
				break;
			case 0:
				proDialog.dismiss();
				myadapter = new MyAdapter();
				myadapter.notifyDataSetChanged();
				lv.setAdapter(myadapter);
				break;
			case 1:
				proDialog.dismiss();
				// setHosInfo();
				break;

			default:
				break;
			}
		};
	};
	

	/**
	 * 自定义listview布局显示内容
	 * 
	 * 
	 */
	class MyAdapter extends BaseAdapter {

		public int getCount() {
			// TODO Auto-generated method stub
			return list_ks.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list_ks.get(position);
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = convertView;
			view = LayoutInflater.from(getApplicationContext()).inflate(
					R.layout.hospital_two_sub, null);
			TextView tv_ks = (TextView) view.findViewById(R.id.tv_ks);
			final String ks =  list_ks.get(position).get("ks").toString();
			tv_ks.setText(ks);

			TextView tv_ksId = (TextView) view.findViewById(R.id.tv_ksId);
			final String ksid = list_ks.get(position).get("ksId").toString();
			tv_ksId.setText(ksid);
			
			TextView tv_kssx = (TextView) view.findViewById(R.id.tv_kssx);
			final String kssx =  list_ks.get(position).get("kssx").toString();
			tv_kssx.setText(kssx);
			
			tv_ks.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(HospitalTwo.this,
							HospitalThree.class);
					intent.putExtra("strId", ksid);
					intent.putExtra("strks", ks);
					intent.putExtra("strkssx", kssx);
					startActivity(intent);

				}
			});
			return view;
		}
	}
	
}
